<?
namespace VSTpl;
function tag_foreach($params)
{
    if(empty($params['from']) or empty($params['item']))
        throw new \Exception('Ошибка в синтаксисе тега {foreach}');

    $var_init = "";
    $counter_init = "";
    $counter_inc = "";

    if(!empty($params['counter'])){
        $counter_init = $params['counter'] . "=0;";
        $counter_inc = $params['counter'] . "++;";
    }

    if(($i = strpos($params['item'], "=>")) === false) $i = 1; else $i += 3;
    $item = substr($params['item'], $i);
    $var_init = "\$this->vars[\"{$item}\"] = \${$item};";

    $result = 
        "<?
            $counter_init
            if(is_array(".$params['from'].") || ".$params['from']." instanceof ArrayAccess)
                foreach (".$params['from']." as ".$params['item']."):
                $counter_inc
        ?>";
    return $result;
}